package com.intexh.quickandroid.module.chat.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.intexh.quickandroid.R;
import com.intexh.quickandroid.base.BaseActivity;
import com.intexh.quickandroid.helper.UserHelper;
import com.intexh.quickandroid.module.chat.bean.GroupListBean;
import com.intexh.quickandroid.net.Apis;
import com.intexh.quickandroid.net.NetworkManager;
import com.intexh.quickandroid.utils.GsonUtils;
import com.intexh.quickandroid.utils.LogCatUtil;
import com.intexh.quickandroid.utils.ValidateUtils;
import com.intexh.quickandroid.utils.glide.GlideHelper;
import com.intexh.quickandroid.widget.LoadMoreBottomView;
import com.intexh.quickandroid.widget.easyadapter.BaseViewHolder;
import com.intexh.quickandroid.widget.easyadapter.RecyclerArrayAdapter;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GroupListActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    private String id;
    private RecyclerArrayAdapter<GroupListBean> mAdapter;
    private int page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
        LogCatUtil.e("frank","id======"+id);
        initRefresh();
        initRecyclerView();
        refreshLayout.startRefresh();
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new RecyclerArrayAdapter<GroupListBean>(mContext) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_group_member_list, parent, false));
            }

            class ViewHolder extends BaseViewHolder<GroupListBean> {
                ImageView  avatar_iv;
                TextView user_name_tv;

                public ViewHolder(View itemView) {
                    super(itemView);
                    avatar_iv = itemView.findViewById(R.id.avatar_iv);
                    user_name_tv = itemView.findViewById(R.id.user_name_tv);
                }

                @Override
                public void setData(GroupListBean data) {
                    GlideHelper.INSTANCE.loadCircleImage(avatar_iv, data.getMember_avatar());
                    user_name_tv.setText(data.getMember_name());
                }
            }
        });
        mAdapter.setOnItemClickListener(position -> {
            GroupListBean item = mAdapter.getItem(position);
            if(UserHelper.getUser().getUserid().equals(item.getMember_id())){
                showToast("不能与自己聊天");
                return;
            }
            AppChatActivity.startActivity(mContext,item.getMember_id(),item.getChat_id(),item.getMember_avatar(),item.getMember_name());
        });
    }

    private void initRefresh() {
        SinaRefreshView header = new SinaRefreshView(mContext);
        header.setArrowResource(R.mipmap.refresh_arrow);
        TextView refreshTextView = header.findViewById(com.lcodecore.tkrefreshlayout.R.id.tv);
        refreshTextView.setTextSize(14);
        refreshLayout.setHeaderView(header);
        LoadMoreBottomView bottom = new LoadMoreBottomView(mContext);
        refreshLayout.setBottomView(bottom);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                page = 1;
                loadData();
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                page++;
                loadData();
            }
        });
    }

    private void loadData() {
        Map<String,String> params= new HashMap<>();
        params.put("id",id);
        params.put("curpage",page+"");
        NetworkManager.INSTANCE.post(Apis.chat_group_members, params, new NetworkManager.OnRequestCallBack() {
            @Override
            public void onOk(String response) {
                String group_member_list = GsonUtils.getStringFromJSON(response,"group_member_list");
                List<GroupListBean> datas = GsonUtils.jsonToBeanList(group_member_list,new TypeToken<List<GroupListBean>>(){}.getType());
                if(page==1){
                    mAdapter.clear();
                    refreshLayout.finishRefreshing();
                }else{
                    refreshLayout.finishLoadmore();
                    if(!ValidateUtils.isValidate(datas))page--;
                }
                mAdapter.addAll(datas);

            }

            @Override
            public void onError(String errorMessage) {
                if(page==1){
                    mAdapter.clear();
                    refreshLayout.finishRefreshing();
                }else{
                    refreshLayout.finishLoadmore();
                    page--;
                }
            }
        });
    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }
}
